/*
 * yocef - Lightweight IoC Event Framework
 *
 * Copyright (c) 2009, Yves Cuillerdier
 * All rights reserved.
 * 
 * This software is licensed under the new BSD License:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
 * 
 * Neither the name of the author nor the names of its contributors may 
 * be used to endorse or promote products derived from this software without 
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * @version 0.8.5
 */
package yocef.client;

import java.util.Map;
import java.util.TreeMap;

/**
 * This class is used to carry the values associated with an event at the time
 * it occurs (For example, a mouseMove event will have <tt>x</tt> and <tt>y</tt>
 * values).
 * <p>
 * Parameters are referred by names.
 */
public class EventParams {

	/** The parameters values */
	private final Map<String, Object> params = new TreeMap<String, Object>();

	/** Create an EventParams object */
	public EventParams() {
		super();
	}

	/**
	 * Set an boolean parameter.
	 * 
	 * @param name
	 *            the parameter name
	 * @param val
	 *            the parameter value
	 * @return this object for easy chaining
	 */
	public EventParams put(final String name, final boolean val) {
		params.put(name, val);
		return this;
	}

	/**
	 * Set an integer parameter.
	 * 
	 * @param name
	 *            the parameter name
	 * @param val
	 *            the parameter value
	 * @return this object for easy chaining
	 */
	public EventParams put(final String name, final int val) {
		params.put(name, val);
		return this;
	}

	/**
	 * Set an double (number) parameter.
	 * 
	 * @param name
	 *            the parameter name
	 * @param val
	 *            the parameter value
	 * @return this object for easy chaining
	 */
	public EventParams put(final String name, final double val) {
		params.put(name, val);
		return this;
	}

	/**
	 * Set an Object (String or whatever) parameter.
	 * 
	 * @param name
	 *            the parameter name
	 * @param val
	 *            the parameter value
	 * @return this object for easy chaining
	 */
	public EventParams put(final String name, final Object val) {
		params.put(name, val);
		return this;
	}

	/**
	 * Get the value for an boolean parameter.
	 * 
	 * @param name
	 *            the parameter name
	 * @return the boolean value: false if value is null, a boolean false, a
	 *         number not equals to 0 or an Object that evaluate to an empty
	 *         String. Else true.
	 */
	public boolean getAsBoolean(final String name) {
		final Object val = params.get(name);
		if (val == null) {
			return false;
		}
		if (val instanceof Integer) {
			return (Integer) val != 0;
		}
		if (val instanceof Double) {
			return (Double) val != 0.0d;
		}
		if (!(val instanceof Boolean)) {
			return !"".equals(val.toString());
		}
		return (Boolean) val;
	}

	/**
	 * Get the value for an integer parameter.
	 * 
	 * @param name
	 *            the parameter name
	 * @return the integer value. Return 0 if value is null, a 0 number, a
	 *         Boolean false or not a number (Integer or Double). Return 1 if
	 *         value is the Boolean true. Else return the number as an integer.
	 */
	public int getAsInt(final String name) {
		final Object val = params.get(name);
		if (val == null) {
			return 0;
		}
		if (val instanceof Boolean) {
			return ((Boolean) val).equals(Boolean.TRUE) ? 1 : 0;
		}
		if (val instanceof Double) {
			return ((Double) val).intValue();
		}
		if (!(val instanceof Integer)) {
			return 0;
		}
		return (Integer) val;
	}

	/**
	 * Get the value for an double (number) parameter.
	 * 
	 * @param name
	 *            the parameter name
	 * @return the double value. Return 0.0d if the value is null, the boolean
	 *         false or not a number. Return 1.0d is value is the boolean true.
	 *         Else return the number as a double.
	 */
	public double getAsDouble(final String name) {
		final Object val = params.get(name);
		if (val == null) {
			return 0.0d;
		}
		if (val instanceof Boolean) {
			return ((Boolean) val).equals(Boolean.TRUE) ? 1.0d : 0.0d;
		}
		if (val instanceof Integer) {
			return (double) ((Integer) val);
		}
		if (!(val instanceof Double)) {
			return 0.0d;
		}
		return (Double) val;
	}

	/**
	 * Get the value for an String parameter.
	 * 
	 * @param name
	 *            the parameter name
	 * @return the String value, an empty string if null, or the value of the
	 *         toString() method for any other kind of Object.
	 */
	public String getAsString(final String name) {
		final Object val = params.get(name);
		if (val == null) {
			return "";
		}
		if (!(val instanceof String)) {
			return val.toString();
		}
		return (String) val;
	}

	/**
	 * Get the value of a parameter.
	 * 
	 * @param name
	 *            the parameter name
	 * @return the parameter value.Could be null
	 */
	public Object get(final String name) {
		return params.get(name);
	}
}
